package cn.HL.pet.service.impl;

import cn.HL.basic.exception.BusinessException;
import cn.HL.basic.util.*;
import cn.HL.org.domain.Shop;
import cn.HL.org.mapper.ShopMapper;
import cn.HL.pet.domain.*;
import cn.HL.pet.dto.AcceptOrderDto;
import cn.HL.pet.mapper.OrderPetAcquisitionMapper;
import cn.HL.pet.mapper.PetMapper;
import cn.HL.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.HL.pet.mapper.SearchMasterMsgMapper;
import cn.HL.pet.service.ISearchMasterMsgService;
import cn.HL.basic.service.impl.BaseServiceImpl;
import cn.HL.user.domain.Logininfo;
import cn.HL.user.domain.User;
import cn.HL.user.mapper.UserMapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HL
 * @since 2022-06-28
 */
@Service
@Slf4j
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private PetMapper petMapper;
    /**
     * 发布宠物信息
     * @param msg
     * @param loginUser
     */
    @Override
    public void publish(SearchMasterMsg msg, Logininfo loginUser) {
        //查寻到user的id
        User user = userMapper.loadByLogininfoId(loginUser.getId());
        msg.setUserId(user.getId());
        //保存发布宠物信息
        searchMasterMsgMapper.save(msg);
        //审核
        String str=msg.getTitle()+msg.getName()+msg.getCoatColor()+msg.getAddress();
        //百度审核
        if(!BaiduAuditUtils.TextCensor(str)){
            //审核不通过
            //修改状态
            msg.setState(-1);
            //生成日志
            SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
            searchMasterMsgAuditLog.setNote("名字,标题,毛色,地址有问题");
            searchMasterMsgAuditLog.setMsgId(msg.getId());
            searchMasterMsgAuditLogMapper.save(searchMasterMsgAuditLog);
            //发送邮件给用户
            EmailUtil.sendSimpleMail("895730310@qq.com","宠物信息发布","你好,您发布的消息含有敏感字符请修改后提交",user.getEmail());
        }else {
            //修改状态
            msg.setState(1);
            //审核通过了
            //获取最近的shop
            //获取用户的点
            Point point = DistanceUtil.getPoint(msg.getAddress());
            //通过点和shop的地址来获取最近的shop
            Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
            if(nearestShop!=null){
                //获取到了最近的店铺
                msg.setShopId(nearestShop.getId());

                //给店铺发送消息
                log.info("老板出来接客咯");
            }
            //没有找到何时的店铺我们就不绑定shop即可
        }
        searchMasterMsgMapper.update(msg);



    }

    /**
     * 接收订单
     * @param dto
     */
    @Override
    public void acceptOrder(AcceptOrderDto dto) {
        //要判断一下这个订单是否被抢了
        OrderPetAcquisition ord =orderPetAcquisitionMapper.loadByMsgIdAndNotCancel(dto.getMsgId());
        //判断是否存在这个订单
        if(ord!=null){
            throw  new BusinessException("订单已被抢请重新获取");
        }
       //先新增宠物
        //通过mgsId获取到宠物的信息
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(dto.getMsgId());
        Pet pet =intopet(msg);
        petMapper.save(pet);
        //新增订单
        ord = intoOrderPetAcquisition(dto, msg, pet);
        orderPetAcquisitionMapper.save(ord);

    }

    /**
     * 拒绝订单
     * @param id
     */
    @Override
    public void reject(Long id) {
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(id);
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
        //还要保存日志哦

    }

    private OrderPetAcquisition intoOrderPetAcquisition(AcceptOrderDto dto, SearchMasterMsg msg, Pet pet) {

        OrderPetAcquisition ord = new OrderPetAcquisition();
        ord.setOrderSn(CodeGenerateUtils.generateOrderSn(dto.getHandler()));
        ord.setDigest(pet.getName()+"订单信息");
        ord.setLastcomfirmtime(new Date(System.currentTimeMillis()+2*24*60*60*1000));
        ord.setPetId(pet.getId());
        ord.setUserId(msg.getUserId());
        ord.setShopId(msg.getShopId());
        ord.setAddress(msg.getAddress());
        ord.setEmpId(dto.getHandler());
        ord.setSearchMasterMsgId(msg.getId());
        return ord;
    }


    private Pet intopet(SearchMasterMsg msg) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setTypeId(msg.getPetType());
        pet.setShopId(msg.getShopId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
